﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Asoft.Web.Models;
using ASoft.Web.Base;

namespace Asoft.Web.Controllers
{
    public class CustomController : EntityController<CustomRepository, Custom>
    {
        [HttpPost]
        public ActionResult Delete()
        {
            var id = int.Parse(Request["CustomId"]);
            Repository.Delete(id);
            return GetJsonResult("success", "删除成功");
        }


        [HttpPost]
        public ActionResult Update()
        {
            var id = int.Parse(Request["CustomId"]);
            var entity = Repository.GetById(id);
            return ProcessEntity(entity, Repository.Update, "更新成功");
        }

        [HttpPost]
        public ActionResult Add()
        {
            var entity = new Custom();
            return ProcessEntity(entity, Repository.Insert, "添加成功");
        }

        public ActionResult ProcessEntity(Custom entity, Action<Custom> action, string msg)
        {

            var status = "error";

            entity.Code = Request["Code"];
            entity.CName = Request["CName"];
            entity.Enable = 1;


            if (string.IsNullOrWhiteSpace(entity.Code))
            {
                return GetJsonResult(status, "编码不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.CName))
            {
                return GetJsonResult(status, "名称不能为空");
            }

            entity.Code = entity.Code.Trim();
            entity.CName = entity.CName.Trim();

            action(entity);

            status = "success";
            return GetJsonResult(status, msg);

        }

        protected override ASoft.Base.PageResult<Custom> GetIndex1PageResult()
        {
            var result = Repository.GetPageResult(null, PageIndex, PageSize, new string[] { "code" }, new string[] { "asc" });
            return result;
        }

        protected override void ProcessJQGridEdit(Custom entity)
        {
            entity.Code = Request["Code"];
            entity.CName = Request["CName"];
            entity.Enable = 1;
        }

        public ActionResult CustomRange()
        {
            var result = CurrentContext.Customs.Select(p => new SelectListItem() { Text = p.CName, Value = p.Code }).ToList();
            var selected = result.FirstOrDefault(p => p.Value == CurrentContext.CustomCode);
            if (selected != null)
            {
                selected.Selected = true;
            }
            return View(result);
        }

        [HttpPost]
        public ActionResult ChangeCurrentCustomCode(string customCode)
        {
            if (CurrentContext.Customs.Count(p => p.Code == customCode) > 0)
            {
                CurrentContext.CustomCode = customCode;
            }
            CurrentContext.Customs.Clear();
            return Json(new { status = "success" });
        }
    }
}
